package com.lynn.redis.service.impl;

import com.alibaba.fastjson.JSON;
import com.lynn.redis.bean.User;
import com.lynn.redis.cache.CacheConfig;
import com.lynn.redis.cache.EhCacheUtils;
import com.lynn.redis.cache.RedisCacheUtils;
import com.lynn.redis.repo.UserRepo;
import com.lynn.redis.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Optional;

/**
 * @ClassName UserServiceImpl
 * @Description TODO
 * @Author ly
 * @Date 2019-06-04 18:00
 * @Version 1.0
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private UserRepo userRepo;

    @Autowired
    private RedisCacheUtils redisCacheUtils;

    @Autowired
    private EhCacheUtils ehCacheUtils;

    @Override
    public User getUserById(String id) {
        String key = this.getClass().getName() + "-" + Thread.currentThread().getStackTrace()[1].getMethodName()
                + "-id:" + id;
        System.out.println(key);
        // 操作缓存
        User cache = (User) ehCacheUtils.get(CacheConfig.EhCacheNames.CACHE_10MINS, key);
        System.out.println("获取缓存:" + cache);
        User u = null;
        if (cache == null) {
            log.info("ehcache 没有");
            String userRedis = (String) redisCacheUtils.get(key);
            if (StringUtils.isEmpty(userRedis)) {
                log.info("redis 没有");
                Optional<User> user = userRepo.findById(id);
                if (user.isPresent()) {
                    u = user.get();
                    ehCacheUtils.put(CacheConfig.EhCacheNames.CACHE_10MINS, key, u);
                    redisCacheUtils.set(key, JSON.toJSONString(u), 300);
                }
            } else {
                log.info("redis 找到");
                u = JSON.parseObject(userRedis, User.class);
                ehCacheUtils.put(CacheConfig.EhCacheNames.CACHE_10MINS, key, u);
            }
        } else {
            log.info("ehcache 找到");
            u = cache;
        }
        return u;
    }

    @Override
    public String init() {
        for (int i = 0; i < 10; i++) {
            User u = User.builder()
                    .id(i + "")
                    .name("name:" + i)
                    .age(i + 20)
                    .build();
            userRepo.save(u);
        }
        return "成功初始化";
    }

    /**
     * 单独使用redis缓存, 不指定key
     * @return
     */
    @Cacheable(cacheManager = CacheConfig.CacheManagerNames.REDIS_CACHE_MANAGER, value = CacheConfig.RedisCacheNames.CACHE_15MINS)
    @Override
    public User getUserFromRedis() {
        String userData = (String) redisCacheUtils.get("2");
        User user = null;
        if (StringUtils.isEmpty(userData)) {
            log.info("redis 没有");
            user = userRepo.findById("2").orElse(null);
            redisCacheUtils.set("user2", JSON.toJSONString(user), 300);
        } else {
            log.info("redis 找到");
            user = JSON.parseObject(userData, User.class);
        }
        return user;
    }


    /**
     * 使用ehcache 存储
     * @param key
     * @return
     */
    @Override
    @Cacheable(key = "'key'", cacheManager = CacheConfig.CacheManagerNames.EHCACHE_CACHE_MAANGER, cacheNames = CacheConfig.EhCacheNames.CACHE_10MINS)
    public String demo1(String key) {
        System.out.println("......demo1");
        return "abc" + key;
    }

    /**
     * redis 存储
     * @param key
     * @return
     */
    @Override
    @Cacheable(key = "'key'", cacheManager = CacheConfig.CacheManagerNames.REDIS_CACHE_MANAGER, cacheNames = CacheConfig.RedisCacheNames.CACHE_15MINS)
    public String demo2(String key) {
        System.out.println("......demo2");
        return "abcdemo2" + key;
    }

}
